# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
from snownlp import SnowNLP
import pandas as pd
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
plt.rcParams['font.sans-serif'] = ['SimHei']#绘制显示中文
plt.rcParams['axes.unicode_minus'] = False
# 获取情感分数
# 读取CSV文件
df = pd.read_csv('香港迪士尼_comments.csv')

line = df["评价"].tolist()
print(len(line))
sentimentslist = []
k = 1
for i in line:
    k +=1
    s = SnowNLP(i)
    print(k,s.sentiments)
    sentimentslist.append(s.sentiments)

# 区间转换为[-0.5, 0.5]
result = []
i = 0
while i < len(sentimentslist):
    result.append(sentimentslist[i] - 0.5)
    i = i + 1

# 添加情感倾向字段
df['情感倾向'] = ['积极' if s > 0.1 else '消极' for s in sentimentslist]
# 添加snownlp得分字段
df['score'] = sentimentslist
# 可视化画图
plt.plot(np.arange(0, len(sentimentslist), 1), result, 'b-')
plt.xlabel('Number')
plt.ylabel('Sentiment')
plt.title('Analysis of Sentiments')
plt.show()

df.to_excel(r'情感分析结果.xlsx', index=False)
